/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-01-14 16:11:23
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:53:59
 */

'use strict';
/**
 * [普通图片列表]
 */
define([
    '../base/baseView',
    '../model/video'
], function(
    BaseView,
    videoModel) {
    function NomarlImg() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function() {
            self.$el = $('#video-container');

            self.event();

            self.params = {
                cPage: 1
            };
            $('[list-manager="ad-video"]').listManager('init', {
                supportAjaxPage: true,
                setTopVal:256,//调整吸顶的高度,需要减去这个高度
                scrollDOM:$('.fmp-article'),//监听滚动条
                pageCallback: function(query) {
                    $.extend(self.params, query)
                    self.queryVideo();
                }
            }, function(query) {
                $.extend(self.params, query)
                self.$el.find('.btn-search').trigger('click');
            });
        }

        this.renderList = function(videoes) {
            var $tableTbody = self.$el.find('.video-table tbody');
            $tableTbody.empty();
            self._.each(videoes, function(vid, idx) {
                vid.id = idx + 1;
                vid.size = self.kit.bytesToSize(vid.size);
                vid.uploadTime = self.kit.formatTime(vid.uploadTime);
                var html = self.t('#t-video-list-tr', vid);
                $tableTbody.append(html);
            });
        }

        this.event = function() {
            self.$el.off('queryVideo').on('queryVideo', $.proxy(self.queryVideo, self));
            self.$el.off('click', '.player-video').on('click', '.player-video', $.proxy(self.playerVideo, self));
        }
        this.queryVideo = function(e, params) {
            if (params) {
                if (!params.appNames) {
                    delete self.params.appNames;
                }
                $.extend(self.params, params);
            }
            videoModel.queryVideoes(self.params, function(results) {
                if (results.adVideoList) {
                    self.renderList(results.adVideoList);
                } else {
                    self.renderList([]);
                }
                self.params.tSize = results.summary ? (results.summary.totalCount ? results.summary.totalCount : 0) : 0;
                var pageData = self.getPageData(self.params.pSize, self.params.tSize, self.params.cPage);
                $('[list-manager="ad-video"]').listManager([{ 'resetTd': false }, { 'resetPageData': pageData }]);
            });
        }
        this.getPageData = function(pSize, tSize, cPage) {
            return {
                tPage: Math.ceil(tSize / pSize), //总页数
                cPage: cPage, //当前页
                pSize: pSize, //每页显示条数
                tSize: tSize
            }
        }
        this.playerVideo = function(e) {
            var title = $(e.currentTarget).data('title');
            var videoUrl = $(e.currentTarget).data('video-url');
            var videoHtml = '<video controls="controls" class="embed-responsive-item"><source type=video/mp4 src="' + videoUrl + '"></video>'
            var $modal = $('#player-video-modal');
            $modal.find('.modal-title').html(title);
            $modal.find('.modal-body div').html(videoHtml);
            $modal.modal('show');
            $modal.off('hide.bs.modal').on('hide.bs.modal', function(e) {
                $modal.find('.modal-body div').empty();
            });
        }


    };
    return new NomarlImg();
});
